package com.gaoyi.basemodule.baseUtil;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.text.TextUtils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class BitmapUtils {

    private static volatile BitmapUtils instance;

    public static BitmapUtils getInstance() {
        if (instance == null) {
            synchronized (BitmapUtils.class) {
                if (instance == null) {
                    instance = new BitmapUtils();
                }
            }
        }
        return instance;
    }

    /**
     * 压缩图片
     */
    public Bitmap resizePath(String srcPath) {
        BitmapFactory.Options newOpts = new BitmapFactory.Options();
        newOpts.inJustDecodeBounds = true;// 开始读入图片，此时把options.inJustDecodeBounds 设回true了
        Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
        resize(newOpts);
        bitmap = BitmapFactory.decodeFile(srcPath, newOpts);
        return bitmap;
    }

    public Bitmap resizeBitmap(Bitmap bitmap) {
        BitmapFactory.Options newOpts = new BitmapFactory.Options();
        //开始读入图片，此时把options.inJustDecodeBounds 设回true了
        newOpts.inJustDecodeBounds = true;
        resize(newOpts);
        return bitmap;
    }

    private void resize(BitmapFactory.Options newOpts) {
        int w = newOpts.outWidth;
        int h = newOpts.outHeight;
        //现在主流手机比较多是800*480分辨率，所以高和宽我们设置为
        float hh = 800f;//这里设置高度为800f
        float ww = 480f;//这里设置宽度为480f
        //缩放比。由于是固定比例缩放，只用高或者宽其中一个数据进行计算即可
        int be = 1;//be=1表示不缩放
        if (w > h && w > ww) {//如果宽度大的话根据宽度固定大小缩放
            be = (int) (w / ww);
        } else if (w < h && h > hh) {//如果高度高的话根据宽度固定大小缩放
            be = (int) (h / hh);
        }
        if (be == 1) {
            be += 1;
        }
        newOpts.inSampleSize = be;//设置缩放比例
        // 设置图片可以缩小
        newOpts.inScaled = true;
        int calSize = Math.min(h, w);
        newOpts.inTargetDensity = (int) ((ww * newOpts.inDensity) / (calSize / newOpts.inSampleSize));
        newOpts.inJustDecodeBounds = false;
        //重新读入图片，注意此时已经把options.inJustDecodeBounds 设回false了
    }

    /**
     * 旋转Bitmap
     *
     * @param angle 旋转角度
     */
    public Bitmap rotatingBitmap(Bitmap targetBitmap, int angle) {
        if (targetBitmap == null) return null;
        Bitmap bmp = null;
        Matrix matrix = new Matrix();
        matrix.postRotate(angle);
        try {
            bmp = Bitmap.createBitmap(targetBitmap, 0, 0, targetBitmap.getWidth(), targetBitmap.getHeight(), matrix, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (bmp == null) bmp = targetBitmap;
        targetBitmap.recycle();
        return bmp;
    }

    public String bitmap2Url(Activity activity, Bitmap bitmap, String url) {
        FileOutputStream fops = null;
        if (TextUtils.isEmpty(url)) {
            File photoFile = new File(activity.getExternalCacheDir(), ImageUtil.getImgName());
            url = photoFile.getAbsolutePath();
        }
        try {
            fops = new FileOutputStream(url);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 80, fops);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fops != null) {
                    fops.flush();
                    fops.close();
                }
                if (bitmap != null && !bitmap.isRecycled()) {
                    bitmap.recycle();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return url;
    }

    public Bitmap url2Bitmap(String path) {
        return url2Bitmap(path, -1);
    }

    /**
     * 图片转Bitmap
     *
     * @param path         图片路径
     * @param inSampleSize 图片的长宽都是原来的1/inSampleSize
     */
    public Bitmap url2Bitmap(String path, int inSampleSize) {
        FileInputStream fis = null;
        BufferedInputStream bis = null;
        Bitmap bm = null;
        try {
            fis = new FileInputStream(path);
            bis = new BufferedInputStream(fis);
            BitmapFactory.Options options = new BitmapFactory.Options();
            if (inSampleSize != -1) {
                // 图片的长宽都是原来的1/8
                options.inSampleSize = inSampleSize;
            }
            bm = BitmapFactory.decodeStream(bis, null, options);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bis != null) {
                    bis.close();
                }
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return bm;
    }
}
